package com.turman.coroutionsample.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.turman.coroutionsample.ui.theme.CoroutionSampleTheme

enum class Router {
    MAINPAGE,
    FLOW_DOWNLOAD,
    FLOW_ROOM,
    FLOW_RETROFIT,
    STATE_FLOW,
    SHARED_FLOW,
    PAGING_FLOW,
    HILT_SAMPLE
}

@Preview
@Composable
fun MainPageView() {
    CoroutionSampleTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            MyNavHost(modifier = Modifier.fillMaxSize())
        }
    }
}

@Composable
fun MyNavHost(modifier: Modifier = Modifier) {
    val navController = rememberNavController()

    NavHost(
        navController = navController,
        startDestination = Router.MAINPAGE.name,
        modifier = modifier
    ) {
        composable(route = Router.MAINPAGE.name) {
            MainList(navController = navController)
        }
        composable(route = Router.FLOW_DOWNLOAD.name) {
            DownloadFlowPage()
        }
        composable(route = Router.FLOW_ROOM.name) {
            RoomFlowPage()
        }
        composable(route = Router.FLOW_RETROFIT.name) {
            RetrofitFlowPage()
        }
        composable(route = Router.STATE_FLOW.name) {
            StateFlowPage()
        }
        composable(route = Router.SHARED_FLOW.name) {
            SharedFlowPage()
        }
        composable(route = Router.PAGING_FLOW.name) {
            PagingFlowView()
        }
        composable(route = Router.HILT_SAMPLE.name) {
            HiltView()
        }
    }
}

@Composable
fun MainList(navController: NavController) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
    ) {
        Text(
            text = "FLOW DOWNLOAD",
            color = MaterialTheme.colors.onBackground,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.LightGray
                )
                .clickable {
                    navController.navigate(Router.FLOW_DOWNLOAD.name)
                }
                .padding(10.dp)
        )
        Spacer(modifier = Modifier.height(10.dp))
        Text(
            text = "FLOW ROOM",
            color = MaterialTheme.colors.onBackground,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.LightGray
                )
                .clickable {
                    navController.navigate(Router.FLOW_ROOM.name)
                }
                .padding(10.dp)
        )
        Spacer(modifier = Modifier.height(10.dp))
        Text(
            text = "FLOW RETROFIT",
            color = MaterialTheme.colors.onBackground,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.LightGray
                )
                .clickable {
                    navController.navigate(Router.FLOW_RETROFIT.name)
                }
                .padding(10.dp)
        )
        Spacer(modifier = Modifier.height(10.dp))
        Text(
            text = "STATEFLOW",
            color = MaterialTheme.colors.onBackground,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.LightGray
                )
                .clickable {
                    navController.navigate(Router.STATE_FLOW.name)
                }
                .padding(10.dp)
        )
        Spacer(modifier = Modifier.height(10.dp))
        Text(
            text = "SHAREDFLOW",
            color = MaterialTheme.colors.onBackground,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.LightGray
                )
                .clickable {
                    navController.navigate(Router.SHARED_FLOW.name)
                }
                .padding(10.dp)
        )
        Spacer(modifier = Modifier.height(10.dp))
        Text(
            text = "PAGINGFLOW",
            color = MaterialTheme.colors.onBackground,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.LightGray
                )
                .clickable {
                    navController.navigate(Router.PAGING_FLOW.name)
                }
                .padding(10.dp)
        )
        Spacer(modifier = Modifier.height(10.dp))
        Text(
            text = "HILTSAMPLE",
            color = MaterialTheme.colors.onBackground,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.LightGray
                )
                .clickable {
                    navController.navigate(Router.HILT_SAMPLE.name)
                }
                .padding(10.dp)
        )
    }
}